package com.lin.bootstudy.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.bootstudy.dao.UserDao;
import com.lin.bootstudy.domain.User;
import com.lin.bootstudy.service.impl.UserSerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/book")
public class Book {

    @Autowired
    private UserSerImpl userSer;

    @GetMapping("/get")
    public Result getBooks(){
        try {
            int error=1/0;
        } catch (Exception e) {
            throw new SystemException(Code.SYSTEM_ERR,"系统繁忙,请再试一次");
        }
        return new Result(Code.GET_OK,null,"这是一条测试数据");
    }


    @GetMapping("/get/{id}")
    public Result getBook(@PathVariable Integer id){
        User user =userSer.selectById(id);
        Integer code = user != null ? Code.GET_OK : Code.GET_ERR;
        String message = user != null ? "查询成功" : "查询失败";
        return new Result(code,user,message);
    }

    @DeleteMapping("delete/{id}")
    public Result deleteBook(@PathVariable Integer id){
        boolean flag=userSer.deleteById(id);
        Integer code = flag ? Code.DELETE_OK : Code.DELETE_ERR;
        String message = flag ? "删除成功" : "删除失败";
        return new Result(code,flag,message);
    }

    @PostMapping("add")
    public Result addBook(@RequestBody User user){
        boolean flag=userSer.insert(user);
        Integer code = flag ? Code.UPDATE_OK : Code.UPDATE_ERR;
        String message = flag ? "更新成功" : "更新失败";
        return new Result(code,flag,message);
    }

    @PutMapping("update")
    public Result updateBook(@RequestBody User user){
        User user1=userSer.selectById(user.getId());
        user1.setUserName("王志鹏");
        boolean  flag= userSer.update(user1);
        Integer code = flag ? Code.UPDATE_OK : Code.UPDATE_ERR;
        String message = flag ? "更新成功" : "更新失败";
        return new Result(code,flag,message);
    }


    @GetMapping("testaop")
    public String testAop(){
        userSer.test();
        return "testAop";
    }
}
